package es;


import com.alibaba.fastjson2.JSON;
import es.esentity.Student;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
public class IndexBuilder {

    @Test
    public void test() throws Exception {
        buildData("student",100);

    }


    public static void buildData(String indexName,int docCount) throws Exception {
        createIndexIfNotExists(indexName);
        List<Student> students = generateStudents(docCount); // 生成100个学生文档
        indexDocuments(students,indexName); // 将学生文档索引到Elasticsearch中
    }
    private static RestHighLevelClient client;// 初始化你的Elasticsearch客户端
    static {
        //TODO 初始化连接
        HttpHost host = new HttpHost("localhost", 9200);
        client = new RestHighLevelClient(RestClient.builder(host));
    }

    private static String MAPPING="{\"properties\":{\"name\":{\"type\":\"text\",\"analyzer\":\"ik_max_word\",\"search_analyzer\":\"ik_smart\"},\"studentNo\":{\"type\":\"keyword\"},\"birthday\":{\"type\":\"date\"},\"location\":{\"type\":\"geo_point\"},\"gender\":{\"type\":\"keyword\"}}}";
    private static void createIndexIfNotExists(String indexName) {
        try{
            //TODO 如果索引不存在 则创建索引 存在则先删除在创建
            GetIndexRequest request = new GetIndexRequest(indexName);
            boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
            //TODO 创建的索引需要指定分片1 副本0 使用书名MAPPING设置映射
            if (exists){
                log.debug("索引已存在,删除索引:{}",indexName);
                DeleteIndexRequest deleteRequest = new DeleteIndexRequest(indexName);
                AcknowledgedResponse response = client.indices().delete(deleteRequest, RequestOptions.DEFAULT);
                log.debug("删除索引结果:{}",response.isAcknowledged());
            }else {
                log.debug("索引不存在,创建索引:{}",indexName);
                CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
                String setting = "{\"number_of_shards\":1,\"number_of_replicas\":0}";
                String mapping = MAPPING;
                createIndexRequest.settings(setting, XContentType.JSON);
                createIndexRequest.mapping(mapping, XContentType.JSON);
                CreateIndexResponse response = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
                log.debug("创建索引结果:{}",response.isAcknowledged());
                log.debug("创建分片结果:{}",response.isShardsAcknowledged());
                log.debug("索引名称:{}",response.index());
            }
        }catch (Exception e){
            log.error("测试环境创建索引:{},出现问题",indexName);
            log.error("具体问题:",e);
        }
    }
    private static void indexDocuments(List<Student> students,String indexName) throws Exception {
        //TODO 使用批量 构造索引文档
        BulkRequest bulkRequest = new BulkRequest();
        for (Student student : students){
            String jsonString = JSON.toJSONString(student);
            IndexRequest indexRequest = new IndexRequest(indexName);
            indexRequest.id(student.getStudentNo());
            indexRequest.source(jsonString, XContentType.JSON);
            bulkRequest.add(indexRequest);
        }
        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        BulkItemResponse[] items = bulk.getItems();
        for (int i =0;i<items.length;i++){
            log.debug(i+"索引文档:{}",items[i].getId());
        }

    }
    private static final String[] GENDERS={"男","女"};
    private static final String[] FAMALY_NAMES={"李","王","张","刘","陈","杨","赵","黄","周","吴"};
    private static final String[] NAMES = {
            "小明", "亮亮", "小红", "芳芳", "小刚", "强强",
            "小李", "丽丽", "小王", "巍巍", "小赵", "昭昭",
            "小周", "舟舟", "小吴", "薇薇", "小郑", "珍珍",
            "小张", "章章", "小黄", "煌煌"
    };

    private static List<Student> generateStudents(int docCount) {
        List<Student> students = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < docCount; i++){
            String name=FAMALY_NAMES[(int)(Math.random()*FAMALY_NAMES.length)]+NAMES[(int)(Math.random()*NAMES.length)];
            String gender=GENDERS[(int)(Math.random()*GENDERS.length)];
            Student student = new Student(
                    name,
                    "2021000" + i,
                    LocalDate.of(2000 + random.nextInt(20), 1, 1),
                    (39.9042 + random.nextDouble() * 0.1)+","+(116.4074 + random.nextDouble() * 0.1)
                    ,gender);
            students.add(student);
        }
        return students;
    }

}
